//
//  EditView.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/18.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "EditView.h"

@implementation EditView

- (instancetype)initWithType:(EditViewType)type
{
    self = [EditView ViewWithXib];
    if (self) {
        [self setType:type];
    }
    return self;
}

-(void)setType:(EditViewType)type
{
    UIColor *contentColor = k666666;
    CGFloat spacingRight = -20;
    switch (type) {
        case Label_EditViewType:
        {
            UILabel *label = [[UILabel alloc]init];
            self.content = label;
            
            label.font = self.label.font;
            label.textColor = k666666;
            
            [self addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.label);
                make.right.equalTo(self).with.offset(spacingRight);
                make.bottom.equalTo(self).with.offset(-8);
            }];
        }
            break;
        case ImageView_EditViewType:
        {
            UIImageView *view1 = [[UIImageView alloc]init];
            UIImageView *view2 = [[UIImageView alloc]init];
            self.imageView1 = view1;
            self.imageView2 = view2;
            
            view1.image = [UIImage imageNamed:@"selectImageIcon"];
            view2.image = [UIImage imageNamed:@"selectImageIcon"];
            view1.userInteractionEnabled = YES;
            view2.userInteractionEnabled = YES;
            
            CGSize size = CGSizeMake(40, 40);
            
            [self addSubview:view1];
            [self addSubview:view2];
            
            [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(view2.mas_left).with.offset(-8);
                make.top.equalTo(self.label);
                make.width.mas_equalTo(size.width);
                make.height.mas_equalTo(size.height);
                make.bottom.equalTo(self).with.offset(-8);
            }];
            [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).with.offset(spacingRight);
                make.top.equalTo(self.label);
                make.width.mas_equalTo(size.width);
                make.height.mas_equalTo(size.height);
                make.bottom.equalTo(self).with.offset(-8);
            }];
            
            UITapGestureRecognizer *zer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zerAction:)];
            UITapGestureRecognizer *zer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zerAction:)];
            
            [view1 addGestureRecognizer:zer1];
            [view2 addGestureRecognizer:zer2];
        }
            break;
        case TextView_EditViewType:
        {
            IQTextView *textView = [[IQTextView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
            self.textView = textView;
            
            self.textView.textColor = contentColor;
            
            [self addSubview:textView];
            [textView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.label).with.offset(5);
                make.top.equalTo(self.label.mas_bottom).with.offset(5);
                make.right.equalTo(self).with.offset(-25);
                make.bottom.equalTo(self).with.offset(-8);
            }];
        }
            break;
        case TextField_EditViewType:
        {
            UITextField *field = [[UITextField alloc]init];
            self.textField = field;
            
            field.textAlignment = NSTextAlignmentRight;
            field.font = self.label.font;
            field.borderStyle = UITextBorderStyleNone;
            field.textColor = contentColor;
            
            [self addSubview:field];
            [field mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).with.offset(spacingRight);
                make.top.equalTo(self.label);
                make.left.equalTo(self.label.mas_right).with.offset(20);
                make.height.equalTo(self.label);
                make.bottom.equalTo(self).with.offset(-8);
            }];
        }
            break;
        case Button_EditViewType:
        {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            self.btn = button;
            button.titleLabel.font = self.label.font;
            
            [button setTitleColor:contentColor forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            [self addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).with.offset(spacingRight);
                make.top.equalTo(self.label);
                make.left.greaterThanOrEqualTo(self.label.mas_right).offset(-spacingRight);
                make.height.equalTo(@18);
                make.bottom.equalTo(self).with.offset(-8);
            }];
            
            UIImageView *imageView = [[UIImageView alloc]init];
            [self addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.label.mas_right).with.offset(5);
                make.centerY.equalTo(button);
            }];
        }
            break;
            
        default:
            break;
    }
}

-(void)zerAction:(UITapGestureRecognizer *)obj
{
    if (self.selectImageAction) {
        if (obj.view == self.imageView1) {
             self.selectImageAction(obj.view,0);
        }else{
            self.selectImageAction(obj.view,1);
        }
       
    }
}

-(void)btnAction:(id)obj
{
    if (self.buttonAction) {
        self.buttonAction(self);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
